<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + '阶段病害'" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-row :gutter="20">
                <el-col :span="24"><el-form-item label="选择品种" prop="plantVarietyId">
                        <el-select v-model="ruleForm.plantVarietyId" class="w100p" filterable placeholder="请选择养殖品种"
                            @change="handleChangePlantVariety">
                            <el-option v-for="item, index in varietyList" :key="index" :label="item.plantVarietyName"
                                :value="item.plantVarietyId"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24"> <el-form-item label="选择阶段" prop="growStageId">
                        <el-select v-model="ruleForm.growStageId" class="w100p" filterable placeholder="请选择阶段"
                            @change="handleChangeGrowthStageName">
                            <el-option v-if="ruleForm.plantVarietyId == item.plantVarietyId"
                                v-for="item, index in growthStageList" :key="index" :label="item.growStageName"
                                :value="item.growStageId"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="病害名称" prop="stageDiseaseName">
                <el-input v-model="ruleForm.stageDiseaseName" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="病害描述" prop="content">
                <el-input v-model="ruleForm.content" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="处理方法" prop="method">
                <el-input v-model="ruleForm.method" type="textarea" maxlength="150" show-word-limit :rows="4" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="事项图片" prop="pictureIdList">
                <UpLoadImage v-if="dialogVisible" @getFileIdList="getFileIdList" :limit="5" :idList="fileIdList">
                </UpLoadImage>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 认</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components/UpLoadImage";
import stagePestManage from '@/api/cropModel/stagePestManage'
export default {
    components: { UpLoadImage },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        varietyList: {
            type: Array,
            default: () => {
                return []
            }
        },
        growthStageList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            addEditApi: '',
            addEditName : '添加',
            ruleForm: {},
            fileIdList: [],
            rules: {
                plantVarietyId: [
                    { required: true, message: '请选择品种', trigger: 'change' }
                ],
                growStageId: [
                    { required: true, message: '请选择阶段', trigger: 'change' }
                ],
                stageDiseaseName: [
                    { required: true, message: '请输入病害名称', trigger: 'change' }
                ],
                content: [
                    { required: true, message: '请输入病害描述', trigger: 'change' }
                ],
                method: [
                    { required: true, message: '请输入病害处理方法', trigger: 'change' }
                ],
                pictureIdList: [
                    { required: true, message: '请上传图片', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        getFileIdList(v) {
            let fileIdList = v.fileIdList
            this.$set(this.ruleForm, 'pictureIdList', fileIdList)
        },

        isHasGrowStage(id) {
            if (!id) return;
            let chooseGrowStage = this.growthStageList.filter(item => {
                return id == item.plantVarietyId
            })
            if (chooseGrowStage.length == 0) {
                this.$message.warning('该品种未配置阶段')
            }
        },
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.ruleForm = { ...this.data };
                this.addEditName = '添加';
                this.addEditApi = stagePestManage.add;
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.addEditApi = stagePestManage.edit;
                this.addEditName = '编辑';
                this.fileIdList = this.data.pictureIdList || [];
            }
            console.log(this.ruleForm)
        },

        handleChangePlantVariety() {
            let plantVarietyName = this.$public.foundObject(this.varietyList, 'plantVarietyId', this.ruleForm.plantVarietyId).plantVarietyName || '';
            this.$set(this.ruleForm, 'plantVarietyName', plantVarietyName);
            this.$set(this.ruleForm, 'growStageId', '');
            this.$set(this.ruleForm, 'growStageName', '');
            this.isHasGrowStage(this.ruleForm.plantVarietyId)
            console.log(plantVarietyName)
        },
        handleChangeGrowthStageName() {
            let growStageName = this.$public.foundObject(this.growthStageList, 'growStageId', this.ruleForm.growStageId).growStageName || '';
            this.$set(this.ruleForm, 'growStageName', growStageName)
            console.log(growStageName)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.fileIdList = [];
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>